﻿using Sustainalytics.Entities;
using System;

namespace Sustainalytics.Insights.Service.Dto
{
    public class InsightDto
	{
        public Insight ToInsight(Guid? userId = null, string username = null)
        {
            return new Insight()
            {
                Id = (Id == Guid.Empty) ? Guid.NewGuid() : Id,
                AddedByUserId = userId ?? AddedByUserId,
                AddedByUserName = string.IsNullOrEmpty(username) ? AddedByUserName : username,
                AddedByUserNameLowerCase = string.IsNullOrEmpty(username) ? AddedByUserName.ToLowerInvariant() : username.ToLowerInvariant(),
                DatePublished = DatePublished,
                DateUnPublished = DateUnPublished,
                InsightTopicId = InsightTopicId,
                InsightSectorId = InsightSectorId,
                FileId = FileId,
                ImageId = ImageId,
                FileS3Name = FileS3Name,
                ImageS3Name = ImageS3Name,
                State = State,
                Title = Title,
                TitleLowercase = Title.ToLowerInvariant(),
                Summary = Summary
            };
        }

	    public static string ComposeFileUrl(Guid id, string host)
	    {
            if (host.Contains("+"))
                host = host.Replace("+", "0.0.0.0");

            if (!host.EndsWith("/"))
                host += "/";
            var hostUri = new Uri(host);
            return (new Uri(hostUri, hostUri.LocalPath + "api/v1/files/" + id)).ToString().ToLowerInvariant();
	    }
	    public static InsightDto FromInsight(Insight insight, string host, bool includeAllData = false)
	    {
            if (host.Contains("+"))
                host = host.Replace("+", "localhost");


	        if (!host.EndsWith("/"))
	            host += "/";
	        var hostUri = new Uri(host);

            var fileName = string.Empty;
            if (insight.FileS3Name != null)
            {
                var fileS3NameSplit = insight.FileS3Name.Split(new[] { '_' }, 2);
                if (fileS3NameSplit.Length > 1)
                    fileName = fileS3NameSplit[1];
            }

            if (includeAllData)
	        {
	            var result = new InsightDto()
	            {
	                AddedByUserName = insight.AddedByUserName,
	                AddedByUserId = insight.AddedByUserId,
	                DatePublished = insight.DatePublished,
	                DateUnPublished = insight.DateUnPublished,
	                Id = insight.Id,
	                InsightTopicId = insight.InsightTopicId,
	                InsightSectorId = insight.InsightSectorId,
	                State = insight.State,
	                Summary = insight.Summary,
	                Title = insight.Title,
 
	            };

	            if (insight.FileId != Guid.Empty)
	            {
	                result.FileId = insight.FileId;
                    result.FileS3Name = insight.FileS3Name;
                    result.FileUrl = (new Uri(hostUri, hostUri.LocalPath + "api/v1/files/" + insight.FileS3Name)).ToString();
                    result.FileName = fileName;
                    
	            }
	            if (insight.ImageId != Guid.Empty)
	            {
                    result.ImageId = insight.ImageId;
	                result.ImageUrl = (new Uri(hostUri, hostUri.LocalPath + "api/v1/files/" + insight.ImageId)).ToString();
                    result.ImageS3Name = insight.ImageS3Name;
                    result.ImageUrl = (new Uri(hostUri, hostUri.LocalPath + "api/v1/files/" + insight.ImageS3Name)).ToString();
	            }

	            return result;
	        }
	        else
	        {
                return new InsightDto()
                {
                    AddedByUserName = insight.AddedByUserName,
                    DatePublished = insight.DatePublished,
                    DateUnPublished = insight.DateUnPublished,
                    Id = insight.Id,
                    InsightTopicId = insight.InsightTopicId,
                    InsightSectorId = insight.InsightSectorId,
                    State = insight.State,
                    Title = insight.Title,
                    FileId = insight.FileId,
                    ImageId = insight.ImageId,
                    FileS3Name = insight.FileS3Name,
                    ImageS3Name = insight.ImageS3Name,
                    FileName = fileName,
                };
	        }
	    }

		public virtual Guid? InsightTopicId { get; set; }
		public virtual string Title { get; set; }
        public virtual DateTime DatePublished { get; set; }
        public virtual Guid Id { get; set; }
        public virtual string Summary { get; set; }
        public virtual InsightState State { get; set; }
        public virtual DateTime DateUnPublished { get; set; }
        public virtual Guid AddedByUserId { get; set; }
        public virtual string AddedByUserName { get; set; }
        public virtual Guid? InsightSectorId { get; set; }
        public virtual string ImageUrl { get; set; }
        public virtual string FileUrl { get; set; }
        public virtual string ImageName { get; set; }
        public virtual string FileName { get; set; }
        public virtual Guid ImageId { get; set; }
        public virtual Guid FileId { get; set; }
        public string ImageS3Name { get; set; }
        public string FileS3Name { get; set; }
    }
}

